#!/usr/bin/python
# -*- coding: UTF-8 -*-

from flask_wtf import Form
from wtforms import StringField, SubmitField, TextAreaField, BooleanField, SelectField, ValidationError
from wtforms.validators import Required, Length, Email, Regexp

from blog.models import Role, User
from flask_pagedown.fields import PageDownField

class NameForm(Form):
    name = StringField('waht is your name?', validators=[Required()])
    submit = SubmitField('Submit')


class EditProfileForm(Form):
    name = StringField(u'真实姓名', validators=[Length(0,64)])
    location = StringField(u'地址', validators=[Length(0,64)])
    about_me = TextAreaField(u'关于我的描述')
    submit = SubmitField(u'提交')


class EditProfileAdminForm(Form):
    email = StringField(u'电子邮件', validators=[Required(), Length(1,64), Email()])
    username = StringField(u'用户名', validators=[Required(), Length(1,64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
                                                                                      u'用户名只可有数字与字母、.、_的组合，必须以字母开头')])
    confirmed = BooleanField(u'确认')
    role = SelectField(u'权限', coerce=int)
    name = StringField(u'真实姓名', validators=[Length(0,64)])
    location = StringField(u'地址', validators=[Length(0,64)])
    about_me = TextAreaField(u'关于我')
    submit = SubmitField(u'提交')

    def __init__(self, user, *args, **kwargs):
        super(EditProfileAdminForm, self).__init__(*args, **kwargs)
        self.role.choices = [(role.id, role.name)
                             for role in Role.query.order_by(Role.name).all()]
        self.user = user

    def validate_email(self, field):
        if field.data != self.user.email and User.query.filter_by(email=field.data).first():
            raise ValidationError(u'该邮箱地址已被注册')

    def validate_username(self, field):
        if field.data != self.user.username and User.query.filter_by(username=field.data).first():
            raise ValidationError(u'该用户名已被使用')


class PostForm(Form):
    body = PageDownField(u'你有什么想要说的？JUST请BB！', validators=[Required()])
    submit = SubmitField('Submit')


class CommentForm(Form):
    body = TextAreaField(u'', validators=[Required()])
    submit = SubmitField(u'提交')